/*
 * main.fx
 *
 * Created on 22.05.2010, 21:46:42
 */

package javafx191labs.kprokin.task03;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.shape.*;
import java.util.Random;
import java.lang.Integer;
import javafx.scene.control.*;
import javafx.scene.Node;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import java.util.HashSet;

/**
 * @author Kirill
 */
var asd: TextBox;
var myLabel: Label;
var myLabel2: Label;
var myLabel3: Label;
var ballscount = 0;
var playerballscount = 0;
def rnd = new Random();
var balls = [];
var balls_set = new HashSet();
var playerballs = [];
var N: Integer;
var maxball: Integer;
var mycircle: Node[];
var myButton: Button;

class mygroupclass extends Group {
        var row = 0;
        var column = 0;
        }
var mygroup1 = new mygroupclass;
var mygroup2 = new mygroupclass;
var mygroup3 = new mygroupclass;

mygroup2.layoutX = 530;
mygroup2.layoutY = 30;
        

function drawmycircle (x: Integer, y: Integer, color: Color, str: String): Node{
        Group{
                content: [
         Circle{
            centerX: x;
            centerY: y;
            radius:18;
            opacity: 0.5;
            fill: color;
            visible:true;
    },
    Text{
            layoutX: x - 14;
            layoutY: y + 7;
            content: str;
    }
    ]
        }
}

// place your code here
Stage {
	title : "MyApp"
	scene: Scene {
		width: 1030
		//height: 480
                height: 550
		content: [
                        VBox{
                                layoutX: 20;
                                layoutY: 30;
                                spacing: 15
                                content: [
                                        asd = TextBox {
                                        columns: 12
                                        text: "10";
                                        selectOnFocus: true
                                        action: function (){
                                                for (node in mygroup1.content) { delete node from mygroup1.content }
                                                for (node in mygroup2.content) { delete node from mygroup2.content }
                                                for (node in mygroup3.content) { delete node from mygroup3.content }
                                                var rndvalue;                                                                                         
                                                playerballs = null;
                                                N = Integer.parseInt(asd.text);
                                                ballscount = 0;
                                                playerballscount = 0;
                                                maxball = 0;
                                                balls_set.clear();                                                
                                                mygroup2.column = 0;
                                                mygroup2.row = 0;
                                                mygroup1.column = 0;
                                                mygroup1.row = 0;
                                                mygroup3.column = 0;
                                                mygroup3.row = 0;
                                                while (balls_set.size()!=N){
                                                        rndvalue = rnd.nextInt(1000*N)+1;
                                                        balls_set.add(rndvalue);
                                                        if (rndvalue >= maxball)
                                                            maxball = rndvalue;                                                        
                                                }
                                                balls = balls_set.toArray();
                                                for(i in [0..N-1]){
                                                     if(i mod 10 == 0){
                                                             mygroup2.row++;
                                                             mygroup2.column = 0;
                                                     }
                                                     mycircle = drawmycircle(30 + mygroup2.column++ * 50, 15 - 50 + 50 * mygroup2.row, Color.BLUE,"{balls[N-1-i]}");
                                                     insert mycircle into mygroup2.content;
                                                }
                                                println("balls_set: {balls_set}");
                                                println("balls: {balls}");
                                                println("size: {sizeof balls}");
                                                println("maxball {maxball}");
                                                myLabel.text = "есть {N} шариков";
                                                myLabel2.text = "вы отложили {playerballscount} шариков";
                                                myLabel3.text = "минимальный шарик равен 1, а максимальный равен {N*1000}";
                                            }
                                        }

                                        Button {
                                                text: "показать следующий шарик";
                                                action: function (){
                                                        if (ballscount == N ){
                                                                myLabel.text = "шарики кончились, вы проиграли";
                                                                for (i in [0..2]){
                                                                    if (playerballs[i]==maxball.toString())
                                                                        myLabel.text = "вы выиграли, максимальный шарик: {maxball}";
                                                                }
                                                        }
                                                        else{
                                                                myLabel.text = "{balls[ballscount]} осталось {N - ballscount - 1} шариков";
                                                                ballscount++;                                                                
                                                                if(mygroup1.column mod 10 == 0){
                                                                     mygroup1.row++;
                                                                     mygroup1.column = 0;
                                                                }
                                                                mycircle = drawmycircle(30 + mygroup1.column++ * 50, 15 + 50 * mygroup1.row, Color.BLUE,"{balls[ballscount-1]}");
                                                                insert mycircle into mygroup1.content;
                                                                delete mygroup2.content[N-ballscount] from mygroup2.content;
                                                        }
                                                }
                                        }                                        
                                        myLabel = Label{                                                
                                        }
                                        myButton = Button {
                                                text: "отложить шарик";
                                                action: function (){                                                        
                                                        if ((ballscount!=0) and (playerballscount<3) and playerballs[playerballscount-1]!=balls[ballscount-1].toString()) {                                                                
                                                                println("ballscount: {ballscount}");
                                                                insert balls[ballscount-1].toString() into playerballs;                                                                
                                                                myLabel2.text = "{playerballs}";
                                                                playerballscount++;
                                                                if(mygroup1.column mod 10 == 1){
                                                                        mygroup1.column = 10;
                                                                        mygroup1.row--;
                                                                }
                                                                else{
                                                                        mygroup1.column--;
                                                                }
                                                                delete mygroup1.content[ballscount-playerballscount] from mygroup1.content;
                                                                mycircle = drawmycircle(30 + mygroup3.column++ * 50, 50 * mygroup3.row, Color.RED,"{balls[ballscount-1]}");
                                                                insert mycircle into mygroup3.content;
                                                            if (playerballscount == 3){
                                                                    myLabel.text = "шарики кончились, вы проиграли";
                                                                    for (i in [0..2]){                                                                        
                                                                        if (playerballs[i]==maxball.toString())
                                                                            myLabel.text = "вы выиграли, максимальный шарик: {maxball}";
                                                                        }
                                                                    ballscount = N;
                                                            }
                                                        }
                                                }
                                                }
                                          myLabel2 = Label{
                                        },
                                        mygroup1,
                                        mygroup3
                                            ]
                            }
                        HBox{
                                layoutX: 250;
                                spacing: 15
                                content: [
                                        myLabel3 = Label{
                                                text:"для начала игры введите число шариков и нажмите Enter";
                                        }
                                        ]

                        }
                        mygroup2
                         ]

	}

}